package com.aneliya.io.xml;

import java.util.UUID;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.aneliya.model.ErrorType;
import com.aneliya.model.Message;
import com.aneliya.model.MessageType;
import com.aneliya.model.User;

public class XMLMessageContentHandler extends DefaultHandler implements ContentHandler {
	
	private Boolean isXMLMessageComplete = false;
	public Boolean getIsXMLMessageComplete() {
		return isXMLMessageComplete;
	}
	
	private Message message;
	public Message getMessage() {
		return message;
	}
	
	private User user;
	
	private boolean inUsername = false;
	private StringBuffer usernameBuffer;

	private boolean inPassword = false;
	private StringBuffer passwordBuffer;
	
	private boolean inMessageData = false;
	private StringBuffer messageDataBuffer;
	
	private boolean inToken = false;
	private StringBuffer tokenBuffer;
	
	public void startDocument() throws SAXException {
		user = null;
		usernameBuffer = null;
		passwordBuffer = null;
		messageDataBuffer = null;
		
		isXMLMessageComplete = false;
		message = new Message(null, null);
	}
	
	public void endDocument() throws SAXException {
		if (user != null) {
			user.setUsername(usernameBuffer.toString());
			user.setPassword(passwordBuffer.toString());
			message.setMessage(user);
		}
		if (messageDataBuffer != null) {
			message.setMessage(messageDataBuffer.toString());
		}
		if (tokenBuffer != null) {
			UUID token = UUID.fromString(tokenBuffer.toString());
			message.setToken(token);
		}
		isXMLMessageComplete = true;
	}
	
	public void startElement(String namespaceURI, String localName, 
			String qualifiedName, Attributes atts) throws SAXException {
		if (localName.equals("message")) {
			
			int typeAttrIndex = atts.getIndex("type");
			if (typeAttrIndex != -1) {
				MessageType messageType = MessageType.valueOf(atts.getValue(typeAttrIndex));
				message.setMessageType(messageType);
			}
			
			int usernameAttrIndex = atts.getIndex("username");
			if (usernameAttrIndex != -1) {
				message.setUsername(atts.getValue(usernameAttrIndex));
			}
			
			int errorTypeAttrIndex = atts.getIndex("error_type");
			if (errorTypeAttrIndex != -1) {
				ErrorType errorType = ErrorType.valueOf(atts.getValue(errorTypeAttrIndex));
				message.setMessage(errorType);
			}
		}
		
		if (localName.equals("token")) {
			inToken = true;
			tokenBuffer = new StringBuffer();
		}
		
		if (localName.equals("user")) {
			user = new User();
		}
		
		if (localName.equals("username")) {
			inUsername = true;
			usernameBuffer = new StringBuffer();
		}
		
		if (localName.equals("password")) {
			inPassword = true;
			passwordBuffer = new StringBuffer();
		}
		
		if (localName.equals("message_data")) {
			inMessageData = true;
			messageDataBuffer = new StringBuffer();
		}
	}

	public void endElement(String namespaceURI, String localName,
				String qualifiedName) throws SAXException {
		if (localName.equals("token")) {
			inToken = false;
		}
		
		if (localName.equals("username")) {
			inUsername = false;
		}
		
		if (localName.equals("password")) {
			inPassword = false;
		}
		
		if (localName.equals("message_data")) {
			inMessageData = false;
		}
	}

	public void characters(char[] ch, int start, int length) throws SAXException {
		for (int i = start; i < start + length; i++) {
			if (inUsername) {
				usernameBuffer.append(ch[i]);
			} else if (inPassword ) {
				passwordBuffer.append(ch[i]);
			} else if (inMessageData) {
				messageDataBuffer.append(ch[i]);
			} else if (inToken) {
				tokenBuffer.append(ch[i]);
			}
		}
	}
	
}
